package vivek.project.pocketmoney.mutualfund.model.utils
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import mx.collections.ArrayCollection;
	import mx.formatters.NumberFormatter;
	import vivek.project.pocketmoney.common.CommonData;
	import vivek.project.pocketmoney.common.utils.StringUtils;
	import vivek.project.pocketmoney.mutualfund.model.MutualFundConstants;
	import vivek.project.pocketmoney.mutualfund.model.MutualFundModel;

	public class MutualFundPriceUpdateWorker extends EventDispatcher
	{

		public function MutualFundPriceUpdateWorker()
		{
			//getNAVValue(mfScheme.sm_schemeid);
		}
		private var URLData:String=new String();
		private var giantCap:String=new String();
		private var histDate:Date=new Date();
		private var largeCap:String=new String();
		private var mfid:int=0;
		private var midCap:String=new String();
		private var mutualFundModel:MutualFundModel=MutualFundModel.getInstance();
		private var navDate:String=new String();
		private const navUrl:String="http://www.valueresearchonline.com/funds/newsnapshot.asp?schemecode=";
		private var navValue:String=new String();
		private var numberFormatter:NumberFormatter=new NumberFormatter();
		private var paramColl:ArrayCollection;
		private const portfolioUrl:String="http://valueresearchonline.com/funds/portfoliovr.asp?schemecode=";
		private var smallCap:String=new String();
		private var tinyCap:String=new String();
		private var urlReq:URLRequest=new URLRequest(navUrl);

		public function getHistoricNAVValue(schemeid:int, day:int=1, month:int=1, year:int=2009):void
		{
			var url:String="http://valueresearchonline.com/port/getentryprice.asp?schemecode=1386&day=6&month=3&year=2008&amount=10000&units=";
			this.mfid=schemeid;
			var textLoader:URLLoader=new URLLoader();
			var textReq:URLRequest=new URLRequest(url);
			textReq.method=URLRequestMethod.POST;
			var variables:URLVariables=new URLVariables();
			variables.schemecod=schemeid;
			variables.day=day;
			variables.month=month;
			variables.year=year;
			variables.amount="10000";
			variables.amount=""
			this.histDate=new Date(year, month, day);
			textReq.data=variables;
			textLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doHistoricalEvent);
			textLoader.addEventListener(IOErrorEvent.IO_ERROR, doHistoricalEvent);
			textLoader.addEventListener(Event.COMPLETE, doHistoricalEvent);
			textLoader.load(textReq);
		}

		public function getHistoricNAVValueMC(schemeid:int, day:int=1, month:int=1, year:int=2009):void
		{
			var url:String="http://www.moneycontrol.com/mf/mf_info/histdata.php?imffid=SN&im_id=MSN012&im_desc=&im_ffid=SN&ff_desc=";
			this.mfid=schemeid;
			var textLoader:URLLoader=new URLLoader();
			var textReq:URLRequest=new URLRequest(url);
			textReq.method=URLRequestMethod.POST;
			var variables:URLVariables=new URLVariables();
			variables.schemecod=schemeid;
			variables.day=day;
			variables.month=month + 1;
			variables.year=year;
			variables.amount="10000";
			variables.amount=""
			this.histDate=new Date(year, month, day);
			textReq.data=variables;
			textLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doHistoricalEventMC);
			textLoader.addEventListener(IOErrorEvent.IO_ERROR, doHistoricalEventMC);
			textLoader.addEventListener(Event.COMPLETE, doHistoricalEventMC);
			textLoader.load(textReq);
		}

		public function getNAVData(mfScheme:Object):void
		{
			getNAVValue(mfScheme.sm_schemeid);
			getHistoricNAVValue(mfScheme.sm_schemeid);
		}

		private function doEvent(evt:Event):void
		{
			switch (evt.type)
			{
				case Event.COMPLETE: /* If the load was successful, create a URLVariables object from the URLLoader.data property and populate the paramColl ArrayCollection object. */
					var ldr:URLLoader=evt.currentTarget as URLLoader;
					URLData=ldr.data;
					getLatestNAVValue();
					mutualFundModel.addMFNAVValueToDatabase(mfid, navValue, navDate);
					mutualFundModel.addMFNAVValueToHistory(mfid, navValue, navDate);
					dispatchEvent(new Event("UpdateCompletedSuccessFully"));
					break;
				case IOErrorEvent.IO_ERROR:
				{
					dispatchEvent(new Event("UpdateCompletedWithError"));
					break;
				}
			}
		}

		private function doHistoricalEvent(evt:Event):void
		{
			switch (evt.type)
			{
				case Event.COMPLETE: /* If the load was successful, create a URLVariables object from the URLLoader.data property and populate the paramColl ArrayCollection object. */
					var ldr:URLLoader=evt.currentTarget as URLLoader;
					URLData=ldr.data;
					var value:String=StringUtils.stripBetweenNOccurance(URLData, "<br>Entry Price (Rs.) :", 1, "<form>");
					value=StringUtils.trimSpecialChar(value);
					trace(mfid + "\t" + value + "\t" + histDate.toDateString());
					mutualFundModel.addMFNAVValueToHistory(mfid, value, CommonData.getDateFormatter().format(histDate));
					break;
				case IOErrorEvent.IO_ERROR:
				{
					dispatchEvent(new Event("UpdateCompletedWithError"));
					break;
				}
			}
		}

		private function doHistoricalEventMC(evt:Event):void
		{
			switch (evt.type)
			{
				case Event.COMPLETE: /* If the load was successful, create a URLVariables object from the URLLoader.data property and populate the paramColl ArrayCollection object. */
					var ldr:URLLoader=evt.currentTarget as URLLoader;
					URLData=ldr.data;
					var value:String=StringUtils.stripBetweenNOccurance(URLData, "NAV : Rs.", 1, "</B>");
					value=StringUtils.trimSpecialChar(value);
					trace(mfid + "\t" + value + "\t" + histDate.toDateString());
					mutualFundModel.addMFNAVValueToHistory(mfid, value, CommonData.getDateFormatter().format(histDate));
					break;
				case IOErrorEvent.IO_ERROR:
				{
					dispatchEvent(new Event("UpdateCompletedWithError"));
					break;
				}
			}
		}

		private function doPortfolioEvent(evt:Event):void
		{
			switch (evt.type)
			{
				case Event.COMPLETE: /* If the load was successful, create a URLVariables object from the URLLoader.data property and populate the paramColl ArrayCollection object. */
					var ldr:URLLoader=evt.currentTarget as URLLoader;
					URLData=ldr.data;
					getMktCap();
					mutualFundModel.updatePortfolioType(mfid, giantCap, largeCap, midCap, smallCap, tinyCap);
					dispatchEvent(new Event("UpdateCompletedSuccessFully"));
					//mutualFundModel.addMFNAVValueToDatabase(mfid,navValue,navDate);
					break;
				case IOErrorEvent.IO_ERROR:
				{
					dispatchEvent(new Event("UpdateCompletedWithError"));
					break;
				}
			}
		}

		private function getLatestNAVValue():void
		{
			navValue=StringUtils.stripBetweenNOccurance(URLData, "<td class=\"right\" width=\"45%\">", 1, "(");
			navDate=StringUtils.stripBetweenNOccurance(URLData, "<td class=\"right\" width=\"45%\">", 1, "</td>");
			navDate=StringUtils.stripBetweenNOccurance(navDate, "(", 1, ")");
			navValue=StringUtils.trimSpecialChar(navValue);
			trace(navValue, navDate);
		}

		private function getMktCap():void
		{
			giantCap=StringUtils.trimSpecialChar(StringUtils.stripBetweenNOccurance(URLData, "<font size=\"1\" class=\"right\">", 2, "</font>"));
			largeCap=StringUtils.trimSpecialChar(StringUtils.stripBetweenNOccurance(URLData, "<font size=\"1\" class=\"right\">", 3, "</font>"));
			midCap=StringUtils.trimSpecialChar(StringUtils.stripBetweenNOccurance(URLData, "<font size=\"1\" class=\"right\">", 4, "</font>"));
			smallCap=StringUtils.trimSpecialChar(StringUtils.stripBetweenNOccurance(URLData, "<font size=\"1\" class=\"right\">", 5, "</font>"));
			tinyCap=StringUtils.trimSpecialChar(StringUtils.stripBetweenNOccurance(URLData, "<font size=\"1\" class=\"right\">", 6, "</font>"));
			if (tinyCap == "")
				tinyCap="0.00";
			if (smallCap == "")
				smallCap="0.00";
			if (giantCap == "")
				giantCap="0.00";
			if (largeCap == "")
				largeCap="0.00";
			if (midCap == "")
				midCap="0.00";
		}

		private function getNAVValue(schemeid:int):void
		{
			this.mfid=schemeid;
			var textLoader:URLLoader=new URLLoader();
			var textReq:URLRequest=new URLRequest(navUrl + schemeid);
			textLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doEvent);
			textLoader.addEventListener(IOErrorEvent.IO_ERROR, doEvent);
			textLoader.addEventListener(Event.COMPLETE, doEvent);
			textLoader.load(textReq);
			if (MutualFundModel.getSchemeForId(schemeid).sm_schemetype == MutualFundConstants.EQUITY)
			{
				var textLoader1:URLLoader=new URLLoader();
				var textReq1:URLRequest=new URLRequest(portfolioUrl + schemeid);
				textLoader1.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doPortfolioEvent);
				textLoader1.addEventListener(IOErrorEvent.IO_ERROR, doPortfolioEvent);
				textLoader1.addEventListener(Event.COMPLETE, doPortfolioEvent);
				textLoader1.load(textReq1);
			}
			else
			{
				dispatchEvent(new Event("UpdateCompletedSuccessFully"));
			}
		}
	}
}